package com.prs.crm.context;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;

import com.opensymphony.xwork2.ActionContext;
import com.prs.crm.domain.hr.Person;

public class AppContext {
	public static final String ACCOUNTCONTEXT_KEY = "com.prs.crm.context.account";

	@SuppressWarnings("unchecked")
	public static AccountContext getAccountContext() {
		Map session = getSessionMap();
		if (session != null && session.containsKey(ACCOUNTCONTEXT_KEY))
			return (AccountContext) session.get(ACCOUNTCONTEXT_KEY);

		else

			return null;

	}

	@SuppressWarnings("unchecked")
	public static AccountContext createAccountContext(Person user) {
		
		AccountContext context = new AccountContext(user);
		
		Map sessionmap = getSessionMap();
		
		if (sessionmap != null) {
		
			sessionmap.put(ACCOUNTCONTEXT_KEY, context);
		  
		}

		return context;
	}

	@SuppressWarnings("unchecked")
	public static SessionMap getSessionMap() {
		ActionContext ctx = ActionContext.getContext();
		if (ctx != null && ctx.get(ServletActionContext.SESSION) != null) {
			SessionMap session = (SessionMap) ctx
					.get(ServletActionContext.SESSION);
			return session;
		} else {
			return null;
		}
	}

	public static HttpServletRequest getHttpServletRequest() {
		return (HttpServletRequest) ActionContext.getContext().get(
				ServletActionContext.HTTP_REQUEST);
	}
}
